package com.An.controller;

import com.An.pojo.*;
import com.An.service.AdminService;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @auther AnYang
 * @date 2022/3/27 15:57
 **/
@Controller
@RequestMapping("/admin")
public class AdminController {
	@Autowired
	private AdminService adminService;

	@RequestMapping("/GetTeacher")
	public String queryTeacher(Model model) {
		PageHelper.startPage(1, 10);
		List<Teacher> teacherList = adminService.getAllTeacher();
		model.addAttribute("teacherList", teacherList);
		return "admin/teacherList";
	}

	@GetMapping("/GetTeacher/{page}")
	public String queryTeacherLimit(Model model, @PathVariable("page") Integer page) {
		int number = adminService.getAllTeacher().size();
		System.out.println(number);
		model.addAttribute("number", number);
		if (page > 1) {
			model.addAttribute("lastPage", true);
		} else {
			model.addAttribute("lastPage", false);
		}
		PageHelper.startPage(page, 10);
		List<Teacher> teacherList = adminService.getAllTeacher();
		if (teacherList.size() <= 10) {
			model.addAttribute("nextPage", false);
		} else {
			model.addAttribute("nextPage", true);
		}
		model.addAttribute("teacherList", teacherList);
		model.addAttribute("page", page);
		return "admin/teacherList";
	}

	//查询学生
	@RequestMapping("/GetClass")
	public String getStudentByClass(Model model) {
		List<ClassInfo> classInfo = adminService.getClassInfo();
		model.addAttribute("classInfo", classInfo);
		return "admin/classInfo";
	}

	//查询教师
	@RequestMapping(value = "/GetTeacherByClass/{page}", method = RequestMethod.POST)
	public String getTeacherByClass(Model model, @RequestParam("className") String className, @PathVariable("page") Integer page) {
		int number = adminService.getTeacherByClass(className).size();
		model.addAttribute("number", number);
		if (page > 1) {
			model.addAttribute("lastPage", true);
		} else {
			model.addAttribute("lastPage", false);
		}
		PageHelper.startPage(page, 10);
		List<Teacher> teacherList = adminService.getTeacherByClass(className);
		if (page * 10 >= number) {
			model.addAttribute("nextPage", false);
		} else {
			model.addAttribute("nextPage", true);
		}
		model.addAttribute("teacherList", teacherList);
		model.addAttribute("page", page);
		return "admin/teacherList";
	}

	@RequestMapping("/getClassTeacherByClass/{page}")
	public String getClassTeacherByClass(Model model, @RequestParam("classid") Integer classid, @PathVariable("page") Integer page) {
		int number = adminService.getClassTeacherByClass(classid).size();
		model.addAttribute("number", number);
		if (page > 1) {
			model.addAttribute("lastPage", true);
		} else {
			model.addAttribute("lastPage", false);
		}
		PageHelper.startPage(page, 10);
		List<ClassTeacher> teacherList = adminService.getClassTeacherByClass(classid);
		if (page * 10 >= number) {
			model.addAttribute("nextPage", false);
		} else {
			model.addAttribute("nextPage", true);
		}
		model.addAttribute("classTeacherList", teacherList);
		model.addAttribute("page", page);
		return "admin/classTeacherList";

	}

	@RequestMapping(value = "/getTeacherByClass/{className}/{page}")
	public String toGetTeacherByClass(Model model, @PathVariable("className") String className, @PathVariable("page") Integer page) {
		int number = adminService.getTeacherByClass(className).size();
		model.addAttribute("number", number);
		if (page > 1) {
			model.addAttribute("lastPage", true);
		} else {
			model.addAttribute("lastPage", false);
		}
		PageHelper.startPage(page, 10);
		List<Teacher> teacherList = adminService.getTeacherByClass(className);
		if (page * 10 >= number) {
			model.addAttribute("nextPage", false);
		} else {
			model.addAttribute("nextPage", true);
		}
		model.addAttribute("teacherList", teacherList);
		model.addAttribute("page", page);
		return "admin/teacherList";
	}

	@RequestMapping("/toArrangeGuardian/{class_name}")
	public String toArrangeGuardian(Model model, @PathVariable("class_name") String class_name) {
		List<Teacher> teacherList = adminService.getTeacherByClass(class_name);
		model.addAttribute("teacherList", teacherList);
		return "admin/arrangeGuardian";
	}

	@GetMapping("/getAllClassTeacher/{page}")
	public String getAllClassTeacher(Model model, @PathVariable("page") Integer page) {
		int number = adminService.getAllClassTeacher().size();
		System.out.println(number);
		model.addAttribute("number", number);
		if (page > 1) {
			model.addAttribute("lastPage", true);
		} else {
			model.addAttribute("lastPage", false);
		}
		PageHelper.startPage(page, 10);
		List<ClassTeacher> classTeacherList = adminService.getAllClassTeacher();
		if (page * 10 >= number) {
			model.addAttribute("nextPage", false);
		} else {
			model.addAttribute("nextPage", true);
		}
		model.addAttribute("classTeacherList", classTeacherList);
		model.addAttribute("page", page);
		return "admin/classTeacherList";
	}

	/*@RequestMapping("/getStudentListByClass/{class_name}/{page}")
	public String showStudentList(@PathVariable("class_name") String class_name,@PathVariable("page") Integer page,Model model){
		int number = adminService.getStudentByClassName(class_name).size();
		model.addAttribute("number",number);
		if (page > 1 && number>10) {
			model.addAttribute("lastPage", true);
		} else {
			model.addAttribute("lastPage", false);
		}
		PageHelper.startPage(page, 10);
		List<Student> studentList = adminService.getStudentByClassName(class_name);
		if (page * 10 >= number) {
			model.addAttribute("nextPage", false);
		} else {
			model.addAttribute("nextPage", true);
		}
		model.addAttribute("List", studentList);
		model.addAttribute("class_name",class_name);
		model.addAttribute("page", page);
		return "admin/studentList";
	}*/
	@RequestMapping("/getStudentListByClass/{classid}")
	public String showStudentList(@PathVariable("classid") Integer classid, Model model) {
		//int number = adminService.getStudentByClassName(class_name).size();
		//model.addAttribute("number",number);
		ClassInfo classInfo = adminService.getClassById(classid);
		List<Student> studentList = adminService.getStudentByClassName(classInfo.getClass_name());
		model.addAttribute("List", studentList);
		//model.addAttribute("class_name",class_name);
		return "admin/studentList";
	}

	@RequestMapping("/getStudent/{sno}")
	public String showStudent(Model model, @PathVariable("sno") Integer sno) {
		Student student = adminService.getStudentBySno(sno);
		model.addAttribute("student", student);
		return "admin/studentInfo";
	}

	//添加教师
	@GetMapping("/toAddTeacher")
	public String toAddTeacher(Model model) {
		List<Course> courseList = adminService.getAllCourse();
		List<ClassInfo> classInfo = adminService.getClassInfo();
		model.addAttribute("classInfo", classInfo);
		model.addAttribute("courseList", courseList);
		return "admin/addTeacher";
	}

	//添加任职
	@GetMapping("/toAddClassTeacher")
	public String toAddClassTeacher(Model model) {
		List<Teacher> teacherList = adminService.getAllTeacher();
		List<Course> courseList = adminService.getAllCourse();
		List<ClassInfo> classInfo = adminService.getClassInfo();
		model.addAttribute("classInfo", classInfo);
		model.addAttribute("courseList", courseList);
		model.addAttribute("teacherList", teacherList);
		return "admin/addClassTeacher";
	}

	@PostMapping("/addClassTeacher")
	public String addClassTeacher(Model model, @RequestParam("tid") Integer tid, @RequestParam("subject") Integer cid, @RequestParam("className") Integer classid) {
		ClassTeacher classTeacher = new ClassTeacher();
		Course course = adminService.getCourseById(cid);
		ClassInfo classInfo = adminService.getClassById(classid);
		Teacher teacher = adminService.getTeacherById(tid);
		classTeacher.setClassid(classInfo.classid);
		classTeacher.setTid(teacher.tid);
		classTeacher.setSubject(course.cname);
		classTeacher.setClass_name(classInfo.class_name);
		classTeacher.setTname(teacher.tname);
		classTeacher.setGender(teacher.gender);
		classTeacher.setClassroom(classInfo.classroom);
		//classTeacher.setStatus(teacher.status);
		teacher.status = teacher.getStatus() + 1;
		classTeacher.setStatus(teacher.status);
		System.out.println(classTeacher);
		ClassTeacher result = adminService.getClassTeacherByClassSubject(classInfo.getClass_name(), course.getCname());
		if (result != null) {
			List<Teacher> teacherList = adminService.getAllTeacher();
			List<Course> courseList = adminService.getAllCourse();
			List<ClassInfo> classInfoList = adminService.getClassInfo();
			model.addAttribute("classInfo", classInfoList);
			model.addAttribute("courseList", courseList);
			model.addAttribute("teacherList", teacherList);
			model.addAttribute("msg", result.getClass_name() + "有" + result.getTname() + "老师在教授" + result.subject + "这门课程");
			return "admin/addClassTeacher";
		} else {
			try {
				adminService.insertClassTeacher(classTeacher);
			} catch (Exception e) {
				teacher.status = teacher.getStatus() - 1;
				List<Teacher> teacherList = adminService.getAllTeacher();
				List<Course> courseList = adminService.getAllCourse();
				List<ClassInfo> classInfoList = adminService.getClassInfo();
				model.addAttribute("classInfo", classInfoList);
				model.addAttribute("courseList", courseList);
				model.addAttribute("teacherList", teacherList);
				model.addAttribute("msg", teacher.getTname() + "已经在" + teacher.getTeacher_class() + "教授" + teacher.getSubject() + "这门课了");
				return "admin/addClassTeacher";
			}
			adminService.updateTeacher(teacher, teacher.getTid());
			return "redirect:/admin/getAllClassTeacher/1";
		}

	}

	@PostMapping("/addTeacher")
	public String addTeacher(Model model,
							 @RequestParam("name") String tname,
							 @RequestParam("gender") String gender,
							 @RequestParam("className") String className,
							 @RequestParam("subject") String subject
	) {
		Teacher teacher = new Teacher();
		teacher.setTeacher_class(className);
		teacher.setSubject(subject);
		teacher.setGender(gender);
		teacher.setTname(tname);
		System.out.println(teacher);
		Teacher result = adminService.getTeacherByClassSubject(teacher.getTeacher_class(), teacher.getSubject());
		//如果不为空，说明该班级的这门课程尚没有老师任职，则将老师信息插入
		//检查教师任职表中是否有老师在授课
		ClassTeacher classTeahcer = adminService.getClassTeacherByClassSubject(className, subject);
		if (classTeahcer != null) {
			Teacher teacherById = adminService.getTeacherById(classTeahcer.getTid());
			//删除任职老师
			Boolean aBoolean = adminService.deleteClassTeacher(classTeahcer.getClassid(), classTeahcer.getTid(), classTeahcer.getSubject());
			Boolean aBoolean2 = adminService.updateTeacherByStatus(teacherById.getTid(), teacherById.getStatus() - 1);
			//加入新的老师
			Boolean aBoolean1 = adminService.insertTeacher(teacher);
			return "redirect:/admin/GetTeacher/1";
		} else if (result != null) {
			// 说明有老师在任职
			List<Course> courseList = adminService.getAllCourse();
			List<ClassInfo> classInfo = adminService.getClassInfo();
			model.addAttribute("classInfo", classInfo);
			model.addAttribute("courseList", courseList);
			model.addAttribute("msg", teacher.getTeacher_class() + "已经有" + result.getTname() + "老师任职" + "请换一个班级或职位");
			return "admin/addTeacher";
		} else {
			//如果为空，则说明班级的这门课程没有老师任职，可以插入。
			Boolean aBoolean = adminService.insertTeacher(teacher);
			//model.addAttribute("msg", "增加老师成功！");
			return "redirect:/admin/GetTeacher/1";
		}
	}

	@GetMapping("/toUpdateTeacher/{id}")
	public String toUpdateTeacher(@PathVariable("id") Integer id, Model model) {
		List<Course> courseList = adminService.getAllCourse();
		List<ClassInfo> classInfo = adminService.getClassInfo();
		Teacher teacher = adminService.getTeacherById(id);
		model.addAttribute("classInfo", classInfo);
		model.addAttribute("courseList", courseList);
		model.addAttribute("teacher", teacher);
		return "admin/updateTeacher";
	}

	@PostMapping("/updateTeacher")
	public String updateTeacher(Model model, Teacher teacher) {
		//查询列表这个班是否有这个课程的老师就职的记录
		System.out.println(teacher);
		//查找原本的教师信息
		Teacher teacherOld = adminService.getTeacherById(teacher.tid);
		//查询教师任职表中这个班是否有老师在教这门课程,就是教师所属的班级的课程是否还在上课.
		ClassTeacher classTeacher = adminService.getClassTeacherByClassSubject(teacher.getTeacher_class(), teacher.getSubject());
		if (classTeacher != null) {
			//说明有记录
			//如果班级和教学科目没有发生变化
			if (Objects.equals(teacherOld.getSubject(), teacher.getSubject()) && Objects.equals(teacherOld.getTeacher_class(), teacher.getTeacher_class())) {
				//就修改教师姓名
				Boolean aBoolean = adminService.updateTeacher(teacher, teacher.getTid());
				if (aBoolean) {
					//session.setAttribute("success","修改成功");
					return "redirect:/admin/GetTeacher";
				} else {
					model.addAttribute("msg", "修改失败");
					List<Course> courseList = adminService.getAllCourse();
					List<ClassInfo> classInfo = adminService.getClassInfo();
					//Teacher teacher = adminService.getTeacherById(id);
					model.addAttribute("classInfo", classInfo);
					model.addAttribute("courseList", courseList);
					model.addAttribute("teacher", teacher);
					return "admin/updateTeacher";
				}
			}
			//这里发现自己所属班级的课程已经不在上课.
			//查询教这门课程的老师
			Teacher objTeacher = adminService.getTeacherById(classTeacher.getTid());
			List<Course> courseList = adminService.getAllCourse();
			List<ClassInfo> classInfo = adminService.getClassInfo();
			model.addAttribute("classInfo", classInfo);
			model.addAttribute("courseList", courseList);
			model.addAttribute("msg", classTeacher.getClass_name() + "有" + objTeacher.getTname() + "老师在教授" + classTeacher.subject + "这门课程");
			model.addAttribute("teacher", teacher);
			return "admin/updateTeacher";
		} else {
			//教师任职表没有查询到相关班级相关课程的任职记录，就直接更改。
			//如果教师的班级被修改了
			//查找原本的教师信息
			//教师班级被修改后,任职表中跟着修改班级号.
			if (!Objects.equals(teacherOld.getTeacher_class(), teacher.getTeacher_class())) {
				//如果班级信息被修改,则找到原有的记录
				//获得修改后班级信息
				ClassInfo classInfoNew = adminService.getClassByName(teacher.getTeacher_class());
				//获得原有的班级信息
				ClassInfo classInfoOld = adminService.getClassByName(teacherOld.getTeacher_class());
				// 考虑这里会不会出现为空的情况 teacherObj,teacher 不会为空,
				ClassTeacher classTeacherNew = adminService.getClassTeacherById(teacherOld.tid, classInfoOld.classid, teacherOld.subject);
				if (classTeacherNew != null) {
					classTeacherNew.setClassid(classInfoNew.classid);
					classTeacherNew.setSubject(teacher.subject);
					classTeacherNew.setGender(teacher.gender);
					classTeacherNew.setTname(teacher.tname);
					classTeacherNew.setClass_name(teacher.getTeacher_class());
					try {
						//session.setAttribute("success","修改成功");
						adminService.updateClassTeacher(classTeacherNew, teacherOld.tid, classInfoOld.classid);
					} catch (Exception e) {
						System.out.println("更新任职表出现错误");
					}
				} else {
					List<Course> courseList = adminService.getAllCourse();
					List<ClassInfo> classInfo = adminService.getClassInfo();
					//Teacher teacher = adminService.getTeacherById(id);
					model.addAttribute("classInfo", classInfo);
					model.addAttribute("courseList", courseList);
					model.addAttribute("teacher", teacher);
					model.addAttribute("msg", "修改失败");
					return "admin/updateTeacher";
				}
			} else {
				ClassTeacher ct = new ClassTeacher();
				ct.setTid(teacher.getTid());
				ct.setTname(teacher.getTname());
				ct.setSubject(teacher.getSubject());
				ct.setGender(teacher.getGender());
				ClassInfo classByName = adminService.getClassByName(teacher.getTeacher_class());
				ct.setClass_name(classByName.getClass_name());
				ct.setClassroom(classByName.getClassroom());
				ct.setClassid(classByName.getClassid());
				teacher.setStatus(teacher.getStatus() + 1);
				try {
					adminService.updateTeacherByStatus(teacher.getTid(), teacher.getStatus());
					ct.setStatus(teacher.getStatus());
					adminService.insertClassTeacher(ct);
				} catch (Exception e) {
					System.out.println("更新出现问题");
					//Teacher objTeacher = adminService.getTeacherById(classTeacher.getTid());
					List<Course> courseList = adminService.getAllCourse();
					List<ClassInfo> classInfo = adminService.getClassInfo();
					model.addAttribute("classInfo", classInfo);
					model.addAttribute("courseList", courseList);
					model.addAttribute("teacher", teacher);
					model.addAttribute("msg", "出现问题,请检查源码!");
					return "admin/updateTeacher";
				}
				return "redirect:/admin/GetTeacher";
			}
			Boolean aBoolean = adminService.updateTeacher(teacher, teacher.getTid());
			if (aBoolean) {
				//session.setAttribute("success","修改成功");
				return "redirect:/admin/GetTeacher";
			} else {
				model.addAttribute("msg", "修改失败");
				return "admin/updateTeacher";
			}
		}

	}

	@RequestMapping("/toDeleteTeacher/{id}")
	public String toDeleteTeacher(@PathVariable("id") Integer id, HttpSession session) {
		Boolean aBoolean = adminService.deleteTeacher(id);
		if (aBoolean) {
			//session.setAttribute("msg","成功删除数据");
			return "redirect:/admin/GetTeacher";
		} else {
			//session.setAttribute("msg","删除失败");
			return "redirect:/admin/GetTeacher";
		}
	}

	@GetMapping("/toDeleteClassTeacher/{classId}/{tid}/{subject}")
	public String toDeleteClassTeacher(@PathVariable("classId") Integer classid, @PathVariable("tid") Integer tid, @PathVariable("subject") String subject) {
		Boolean aBoolean = adminService.deleteClassTeacher(classid, tid, subject);
		Teacher teacher = adminService.getTeacherById(tid);
		teacher.status = teacher.getStatus() - 1;
		adminService.updateTeacher(teacher, teacher.getTid());
		return "redirect:/admin/getAllClassTeacher/1";
	}

	@RequestMapping("/courseArrange/{classId}/{day_id}")
	public String toCourseArrange(Model model, @PathVariable("classId") Integer classid, @PathVariable("day_id") Integer day_id) {
		//获得一周上课的时间
		List<Week> weeKList = adminService.getWeeKList();
		List<Lesson> lessonList = adminService.getLessonList();
		ClassInfo classInfo = adminService.getClassById(classid);
		List<ClassCourse> courseList = adminService.getClassCourseByClass(classid, day_id);
		Week week = adminService.queryDateById(day_id);
		List<ClassInfo> classInfoList = adminService.getClassInfo();
		model.addAttribute("weekList", weeKList);
		model.addAttribute("lessonList", lessonList);
		model.addAttribute("courseInfoList", courseList);
		model.addAttribute("classInfo", classInfo);
		model.addAttribute("classInfoList", classInfoList);
		model.addAttribute("date", week);
		/*List<ClassTeacher> classTeacherList = adminService.getClassTeacherByClass(classid);
		model.addAttribute("timeList", CourseUtils.timeList);
		model.addAttribute("classTeacherList",classTeacherList);*/
		return "admin/courseArrange";
	}

	@RequestMapping("/toArriveCourseArrange")
	public String toGetClassCourse(@RequestParam("classId") Integer classId, @RequestParam("day_id") Integer day_id) {
		return "redirect:/admin/courseArrange/" + classId + "/" + day_id;
	}

	@RequestMapping("/arrangeCourse/{classId}/{day_id}/{lesson_id}")
	public String arrangeCourse(Model model, @PathVariable("classId") Integer classId, @PathVariable("day_id") Integer day_id, @PathVariable("lesson_id") Integer lesson_id) {
		ClassCourse classCourse = adminService.getClassCourseById(classId, day_id, lesson_id);
		List<CourseArrange> teacherList = adminService.getListCourseArrangeByClass(classCourse.getClassid(), day_id);
		List<Week> weeKList = adminService.getWeeKList();
		List<Lesson> lessonList = adminService.getLessonList();
		ClassInfo classInfo = adminService.getClassById(classId);
		List<ClassCourse> courseList = adminService.getClassCourseByClass(classId, day_id);
		Week week = adminService.queryDateById(day_id);
		List<ClassInfo> classInfoList = adminService.getClassInfo();
		model.addAttribute("weekList", weeKList);
		model.addAttribute("lessonList", lessonList);
		model.addAttribute("courseInfoList", courseList);
		model.addAttribute("classInfo", classInfo);
		model.addAttribute("classInfoList", classInfoList);
		model.addAttribute("date", week);
		model.addAttribute("classCourse", classCourse);
		model.addAttribute("teacherList", teacherList);
		return "admin/arrangeCourse";
	}

	@RequestMapping("/getDormApplication")
	public String getStuDormApplication(Model model) {
		List<StudentDorm> studentDormList = adminService.getStudentByDormIdIsNull();
		int size = studentDormList.size();
		model.addAttribute("size", size);
		model.addAttribute("List", studentDormList);
		return "admin/operateDorm";
	}

	@RequestMapping("/toEditorDormApplication/{sno}")
	public String toEditorDormApplication(Model model, @PathVariable("sno") Integer sno) {
		StudentDorm studentDorm = adminService.getStuDormBySno(sno);
		model.addAttribute("student", studentDorm);
		List<Dormitory> dormitoryList = adminService.getDormByGender(studentDorm.getDorm_gender());
		model.addAttribute("List", dormitoryList);
		return "admin/editorDormApplication";
	}

	@PostMapping("/editorStuDorm")
	public String editorStuDorm(@RequestParam("sno") Integer sno, @RequestParam("dorm_id") Integer dorm_id, @RequestParam("bed_id") Integer bed_id) {
		StudentDorm stuDorm = adminService.getStuDormBySno(sno);
		Dormitory dorm = adminService.getDormById(dorm_id);
		stuDorm.setBed_id(bed_id);
		stuDorm.setDorm_id(dorm_id);
		stuDorm.setDorm_number(dorm.getDorm_number());
		stuDorm.setDormPlace(dorm.getDormPlace());
		adminService.updateStuDorm(stuDorm, sno);
		return "redirect:/admin/getDormApplication";
	}

	@RequestMapping("/getStudentDorm/{page}/{size}")
	public String getStudentDorm(@PathVariable("page") Integer page, @PathVariable("size") Integer size, Model model) {
		int number = adminService.getStudentDorm().size();
		model.addAttribute("number", number);
		if (page > 1) {
			model.addAttribute("lastPage", true);
		} else {
			model.addAttribute("lastPage", false);
		}
		PageHelper.startPage(page, size);
		List<StudentDorm> studentDorm = adminService.getStudentDorm();
		if (page * size >= number) {
			model.addAttribute("nextPage", false);
		} else {
			model.addAttribute("nextPage", true);
		}
		model.addAttribute("List", studentDorm);
		model.addAttribute("page", page);
		model.addAttribute("size", size);
		return "admin/studentDorm";
	}

	@RequestMapping("/getQuitStuDorm")
	public String getQuitStuDorm(Model model) {
		List<StudentApplication> studentApplication = adminService.getStudentApplication();
		int size = studentApplication.size();
		/*List<StudentDorm> stuDorm = adminService.getQuitStuDorm();*/
		/*int size = stuDorm.size();*/
		//model.addAttribute("sign",'1');
		model.addAttribute("size", size);
		model.addAttribute("List", studentApplication);
		return "admin/leaveDormApplication";

	}

	@RequestMapping("/getUpdateStuDorm")
	public String getUpdateStuDormList(Model model) {
		List<StudentApplication> studentApplicationList = adminService.getStuApplyUpDorm();
		int size = studentApplicationList.size();

		HashMap<StudentApplication, StudentDorm> map = new HashMap<>();
		for (StudentApplication studentApplication : studentApplicationList) {
			StudentDorm stuDorm = adminService.getStuDormBySno(studentApplication.getSno());
			map.put(studentApplication, stuDorm);
		}
		//model.addAttribute("sign",'2');
		Set<Map.Entry<StudentApplication, StudentDorm>> set = map.entrySet();
		model.addAttribute("size", size);
		model.addAttribute("List", studentApplicationList);
		model.addAttribute("map", set);
		return "admin/updateDormApplication";
	}

	@RequestMapping("/getExamSubjectList/{exam_id}")
	public String getExamSubjectList(Model model, @PathVariable("exam_id") Integer exam_id) {
		List<Exam> examList = adminService.getExamList();
		List<ExamSubject> examSubjectList = adminService.getExamSubjectByExamId(exam_id);
		Exam examById = adminService.getExamById(exam_id);
		model.addAttribute("examById", examById);
		model.addAttribute("examList", examList);
		model.addAttribute("examSubject", examSubjectList);
		return "admin/examArrange";
	}

	@RequestMapping("/toExamSubjectList")
	public String toExamSubjectList(@RequestParam("exam_id") Integer exam_id) {
		return "redirect:/admin/getExamSubjectList/" + exam_id;
	}

	/*@RequestMapping("/toAddExamStudent/{id}")
	public String toAddExamStudent(Model model,@PathVariable("id") Integer id){
		ExamInfo examInfo = adminService.getExamInfo(id);
		model.addAttribute("exam",examInfo);
		adminService
	}*/
	@RequestMapping("/toArrangeExamTime/{exam_id}/{exam_cid}")
	public String toArrangeExamTime(Model model, @PathVariable("exam_id") Integer exam_id, @PathVariable("exam_cid") Integer exam_cid) {
		ExamSubject exam = adminService.getExamSubject(exam_id, exam_cid);
		List<ExamDuration> durationList = adminService.getExamDurationList();
		model.addAttribute("timeList", durationList);
		model.addAttribute("exam", exam);
		return "admin/arrangeExamTime";
	}

	@RequestMapping("/toArrangeTeacher/{exam_id}/{exam_cid}/{exam_place}")
	public String toArrangeTeacher(Model model, @PathVariable("exam_id") Integer exam_id, @PathVariable("exam_cid") Integer exam_cid, @PathVariable("exam_place") String exam_place) {
		//ExamSubject exam = adminService.getExamSubject(exam_id, exam_cid);
		ExamInfo exam = adminService.getExamInfoById(exam_id, exam_cid, exam_place);
		List<Teacher> teacherList = adminService.getAllTeacher();
		model.addAttribute("teacherList", teacherList);
		model.addAttribute("exam", exam);
		return "admin/examArrangeTeacher";
	}

	@RequestMapping("/arrangeTeacher")
	public String arrangeTeacher(Model model, @RequestParam("id") Integer id, @RequestParam("tid_first") Integer tid_first, @RequestParam("tid_second") Integer tid_second, @RequestParam("exam_id") Integer exam_id, @RequestParam("exam_cid") Integer exam_cid, @Param("exam_place") String exam_place) {
		Teacher teacherFirst = adminService.getTeacherById(tid_first);
		Teacher teacherSecond = adminService.getTeacherById(tid_second);
		try {
			adminService.updateExamTeacher(id, tid_first, tid_second, teacherFirst.getTname(), teacherSecond.getTname());
		} catch (Exception e) {
			model.addAttribute("error", "这个老师在同一时间内不能监考在两个考场");
			ExamInfo exam = adminService.getExamInfoById(exam_id, exam_cid, exam_place);
			List<Teacher> teacherList = adminService.getAllTeacher();
			model.addAttribute("teacherList", teacherList);
			model.addAttribute("exam", exam);
			return "admin/examArrangeTeacher";
		}

		return "redirect:/admin/showExamList/" + exam_id + "/" + 1;
	}

	@RequestMapping("/arrangeExamTime")
	public String arrangeExamTime(Model model, @RequestParam("exam_id") Integer exam_id, @RequestParam("exam_cid") Integer exam_cid, @RequestParam("exam_date") String exam_date, @RequestParam("exam_time") String exam_time) {
		System.out.println("exam_date:" + exam_date);
		System.out.println("exam_cid:" + exam_cid);
		System.out.println("exam_id:" + exam_id);

		try {
			adminService.updateStuExam(exam_id, exam_cid, exam_date, exam_time);
		} catch (Exception e) {
			System.out.println("同步学生出现问题");
			System.out.println("请检查SQL是否写错");
			model.addAttribute("error", "同步考生考试时间出现问题!请检查数据库");
			return "admin/arrangeExamTime";
		}
		//同步学生的考试时间
		try {
			adminService.updateExamInfo(exam_id, exam_cid, exam_date, exam_time);
		} catch (Exception e) {
			System.out.println("更新考试时间出现了问题!");
			model.addAttribute("error", "考试时间出现冲突,请检查考试日期和时间!");
			return "admin/arrangeExamTime";
		}
		return "redirect:/admin/showExamList/" + exam_id + "/" + 1;
	}

	@RequestMapping("/toArrangeExamTeacher/{exam_id}/{exam_cid}")
	public String toArrangeExamTeacher(Model model, @PathVariable("exam_id") Integer exam_id, @PathVariable("exam_cid") Integer exam_cid) {
		List<ExamInfo> examInfoList = adminService.getExamInfoList(exam_id, exam_cid);
		model.addAttribute("exam_id", exam_id);
		model.addAttribute("examInfoList", examInfoList);
		return "admin/examPlaceList";
	}

	@RequestMapping("/toShowExamList")
	public String toShowExamList(@RequestParam("exam_id") Integer exam_id) {
		return "redirect:/admin/showExamList/" + exam_id + "/" + 1;
	}

	@RequestMapping("/showExamList/{exam_id}/{page}")
	public String showExamList(Model model, @PathVariable("exam_id") Integer exam_id, @PathVariable("page") Integer page) {
		int number = adminService.getExamInfoListById(exam_id).size();
		model.addAttribute("number", number);
		if (page > 1) {
			model.addAttribute("lastPage", true);
		} else {
			model.addAttribute("lastPage", false);
		}
		//查询考场信息
		List<Exam> examList = adminService.getExamList();
		PageHelper.startPage(page, 10);
		List<ExamInfo> examInfoList = adminService.getExamInfoListById(exam_id);
		Exam examById = adminService.getExamById(exam_id);
		if (page * 10 >= number) {
			model.addAttribute("nextPage", false);
		} else {
			model.addAttribute("nextPage", true);
		}
		model.addAttribute("page", page);
		model.addAttribute("examInfoList", examInfoList);
		model.addAttribute("examById", examById);
		model.addAttribute("examList", examList);
		return "admin/examInfoList";
	}

	@RequestMapping("/arrangeStuExamPlace/{exam_id}")
	public String arrangeStuExamPlace(@PathVariable("exam_id") Integer exam_id, Model model) {
		try {
			Boolean aBoolean = adminService.updateClearStuExamPlace(exam_id);
		} catch (Exception e) {
			System.out.println("清空考场失败！");
		}
		int number = 30;
		List<ClassInfo> classInfoList = adminService.getClassInfo();
		int size = classInfoList.size();
		Set<Integer> set = new HashSet<>();
		for (int i = 1; i <= size; i++) {
			for (int j = 1; j <= number; j++) {
				//加密
				int num = i * 100 + j;
				//System.out.println(num);
				set.add(num);
			}
		}
		System.out.println("=====");
		ArrayList<Integer> list = new ArrayList(set);
		/*int randomIndex = new Random().nextInt(list.size());
		Integer randomItem = (Integer) list.get(randomIndex);*/
		for (ClassInfo classInfo : classInfoList) {
			List<Student> studentList = adminService.getStudentByClassName(classInfo.getClass_name());
			for (Student s : studentList) {
				int randomIndex = new Random().nextInt(list.size());
				//获得set集合中的元素
				Integer integer = list.get(randomIndex);
				System.out.println(integer);
				//set.remove(integer);
				list.remove(randomIndex);
				//解码
				int i = integer / 100;
				int j = integer % 100;
				ClassInfo classById = adminService.getClassById(i);
				try {
					adminService.updateStuExamPlace(exam_id, s.getSno(), classById.getClassroom(), j);
				} catch (Exception e) {
					adminService.updateClearStuExamPlace(exam_id);
					System.out.println("清空考场");
					System.out.println("测试失败!");
				}

			}
		}
		model.addAttribute("success", "成功给所有学生安排考场座位");
		return "redirect:/admin/showExamList/" + exam_id + "/" + 1;
	}

	@RequestMapping("/getStuExamGradeList/{exam_id}/{exam_cid}/{page}")
	public String getStuExamGradeList(@PathVariable("exam_id") Integer exam_id, @PathVariable("exam_cid") Integer exam_cid, @PathVariable("page") Integer page, Model model) {
		int number = adminService.getExamInfoByExamId(exam_id, exam_cid).size();
		model.addAttribute("number", number);
		if (page > 1) {
			model.addAttribute("lastPage", true);
		} else {
			model.addAttribute("lastPage", false);
		}
		PageHelper.startPage(page, 13);
		List<StudentExam> examInfoByExamId = adminService.getExamInfoByExamId(exam_id, exam_cid);
		if (page * 13 >= number) {
			model.addAttribute("nextPage", false);
		} else {
			model.addAttribute("nextPage", true);
		}
		List<ExamSubject> examSubjectList = adminService.getExamSubjectByExamId(exam_id);
		List<Exam> examList = adminService.getExamList();
		model.addAttribute("examList", examList);
		model.addAttribute("exam_id", exam_id);
		model.addAttribute("exam_cid", exam_cid);
		model.addAttribute("examSubjectList", examSubjectList);
		model.addAttribute("stuExamInfoList", examInfoByExamId);
		model.addAttribute("page", page);
		return "admin/stuExamGradeList";
	}

	@RequestMapping("/showStuExamInfo/{exam_id}/{exam_cid}/{classid}")
	public String showStuExamInfo(@PathVariable("exam_id") Integer exam_id, @PathVariable("exam_cid") Integer exam_cid, @PathVariable("classid") Integer classid, Model model) {
		List<StudentExam> examInfoByClass = adminService.getExamInfoByClass(exam_id, exam_cid, classid);
		ClassInfo classById = adminService.getClassById(classid);
		List<ClassInfo> classInfoList = adminService.getClassInfo();
		List<ExamSubject> examSubjectList = adminService.getExamSubjectByExamId(exam_id);
		List<Exam> examList = adminService.getExamList();
		model.addAttribute("examList", examList);
		model.addAttribute("exam_id", exam_id);
		model.addAttribute("exam_cid", exam_cid);
		model.addAttribute("examSubjectList", examSubjectList);
		model.addAttribute("stuExamInfoList", examInfoByClass);
		model.addAttribute("classById", classById);
		model.addAttribute("classInfoList", classInfoList);
		return "admin/showStuExamInfo";
	}

	@RequestMapping("/toShowStuGradeList")
	public String toShowStuGradeList(@RequestParam("exam_id") Integer exam_id, @RequestParam("exam_cid") Integer exam_cid) {
		return "redirect:/admin/getStuExamGradeList/" + exam_id + '/' + exam_cid + '/' + 1;
	}

	@RequestMapping("/toShowStuExamInfo")
	public String toShowStuExamInfo(@RequestParam("exam_id") Integer exam_id, @RequestParam("exam_cid") Integer exam_cid, @RequestParam("classid") Integer classid) {
		return "redirect:/admin/showStuExamInfo/" + exam_id + "/" + exam_cid + "/" + classid;
	}

	@RequestMapping("/toAddStuExamGrade/{exam_id}/{exam_cid}/{sno}")
	public String toAddStuExamGrade(@PathVariable("exam_id") Integer exam_id, @PathVariable("exam_cid") Integer exam_cid, @PathVariable("sno") Integer sno, Model model) {
		StudentExam stuExamInfo = adminService.getStuExamInfo(exam_id, exam_cid, sno);
		model.addAttribute("exam", stuExamInfo);
		return "admin/addStuExamGrades";

	}

	@RequestMapping("/addStuExamGrade")
	public String addStuExamGrade(Model model, @RequestParam("exam_id") Integer exam_id, @RequestParam("exam_cid") Integer exam_cid, @RequestParam("sno") Integer sno, @RequestParam("grade") Integer grade) {
		StudentExam stuExamInfo = adminService.getStuExamInfo(exam_id, exam_cid, sno);
		Student student = adminService.getStudentBySno(sno);
		try {
			adminService.updateStuGrade(exam_id, exam_cid, sno, grade);
		} catch (Exception e) {
			System.out.println("出现问题");
			model.addAttribute("error", "修改成绩出现问题");
			model.addAttribute("exam", stuExamInfo);
			return "admin/addStuExamGrades";
		}
		ClassInfo info = adminService.getClassByName(student.getSclass());
		return "redirect:/admin/showStuExamInfo/" + exam_id + "/" + exam_cid + "/" + info.getClassid();
	}

	@RequestMapping("/ajax/judgeExamTeacher")
	@ResponseBody
	public Boolean AjaxJudgeExamTeacher(Integer tid_first, Integer tid_second, Integer exam_cid, Integer exam_id) {
		List<ExamInfo> examInfoList = adminService.getExamInfoList(exam_id, exam_cid);
		ArrayList<Integer> tidList = new ArrayList<>();
		for (ExamInfo examInfo : examInfoList) {
			if (examInfo.getTid_first() != null) {
				tidList.add(examInfo.getTid_first());
			}
			if (examInfo.getTid_second() != null) {
				tidList.add(examInfo.getTid_second());
			}
		}
		if (!tidList.contains(tid_first)) {
			return !tidList.contains(tid_second);
		} else {
			return false;
		}

	}

	/*@RequestMapping("/ajax/judgeExamTeacherIsNull")
	@ResponseBody
	public Boolean AjaxJudgeTeacherIsNull(){

	}*/
	@RequestMapping("/ajax/getTeacher")
	@ResponseBody
	public Map<String, String> AjaxGetTeacher(Integer tid) {
		HashMap<String, String> map = new HashMap<>();
		Teacher teacher = adminService.getTeacherById(tid);
		map.put("name", teacher.getTname());
		//ClassInfo classInfo = adminService.getClassByName(teacher.getTeacher_class());
		map.put("subject", teacher.getSubject());
		//Course course = adminService.getCourseByName(teacher.getSubject());
		map.put("class_name", teacher.getTeacher_class());
		return map;
	}

	@RequestMapping("/ajax/getDorm")
	@ResponseBody
	public Dormitory AjaxDorm(Integer dorm_id) {
		return adminService.getDormById(dorm_id);
	}

	@RequestMapping("/ajax/getStudentDorm")
	@ResponseBody
	public Boolean AjaxStuDorm(Integer dorm_id, Integer bed_id) {
		StudentDorm studentDorm = adminService.getStuByDormAndBedId(dorm_id, bed_id);
		return studentDorm == null;
	}

	@RequestMapping("/ajax/agreeApplication")
	@ResponseBody
	public Boolean AjaxDeleteStuDorm(Integer sno) {
		Boolean aBoolean = adminService.deleteStuDormBySno(sno);
		Boolean aBoolean1 = adminService.deleteStuApplyBySno(sno);
		return (aBoolean1 && aBoolean) == aBoolean;
	}

	@RequestMapping("/ajax/refuseApplication")
	@ResponseBody
	public Boolean AjaxUpdateStudDorm(Integer sno) {
		StudentDorm dorm = adminService.getStuDormBySno(sno);
		dorm.setSign(0);
		Boolean aBoolean = adminService.deleteStuApplyBySno(sno);
		Boolean aBoolean1 = adminService.updateStuDorm(dorm, sno);
		return (aBoolean && aBoolean1) == aBoolean;
	}

	@RequestMapping("/ajax/judgeStuDormNumber")
	@ResponseBody
	public Boolean AjaxJudgeStuDorm(Integer dorm_id) {
		List<StudentDorm> dormList = adminService.getStudentDormById(dorm_id);
		Dormitory dorm = adminService.getDormById(dorm_id);
		int size = dormList.size();
		return dorm.getDorm_number() > size;
	}

	@RequestMapping("/ajax/agreeUpdateDorm")
	@ResponseBody
	public Boolean AjaxAgreeUpdateDorm(Integer sno) {
		StudentDorm studentDorm = adminService.getStuDormBySno(sno);
		StudentApplication application = adminService.getStuApplicationBySno(sno);
		Boolean aBoolean = adminService.deleteStuApplyBySno(sno);
		Boolean aBoolean1 = adminService.deleteStuApplication(sno);
		studentDorm.setDorm_id(application.getDorm_id());
		studentDorm.setDormPlace(application.getDormPlace());
		studentDorm.setSign(0);
		studentDorm.setBed_id(application.getBed_id());
		Boolean aBoolean2 = adminService.updateStuDorm(studentDorm, sno);
		if ((aBoolean2 && aBoolean1) && aBoolean) {
			return true;
		} else {
			return false;
		}


	}

	@RequestMapping("/ajax/refuseUpdateDorm")
	@ResponseBody
	public Boolean AjaxRefuseUpdateDorm(Integer sno) {
		/*StudentApplication application= map.getKey();
		StudentDorm studentDorm = map.getValue();*/
		StudentDorm studentDorm = adminService.getStuDormBySno(sno);
		StudentApplication application = adminService.getStuApplicationBySno(sno);
		Boolean aBoolean = adminService.deleteStuApplyBySno(sno);
		Boolean aBoolean1 = adminService.deleteStuApplication(sno);
		studentDorm.setSign(0);
		Boolean aBoolean2 = adminService.updateStuDorm(studentDorm, sno);
		if ((aBoolean2 && aBoolean1) && aBoolean) {
			return true;
		} else {
			return false;
		}

	}

	@RequestMapping("/ajax/arrangeCourse")
	@ResponseBody
	public Boolean AjaxArrangeCourse(Integer classId, Integer lesson_id, Integer day_id, String subject) {
		Boolean bBoolean = true;
		//确定班级和时间
		ClassCourse classCourse = adminService.getClassCourseById(classId, day_id, lesson_id);
		//确定课程得到授课老师
		CourseArrange courseArrange = adminService.getCourseArrangeByClassSubject(classId, subject, day_id);
		//判断该时间的老师是否已经安排课程
		ClassCourse classCourseByTid = adminService.getClassCourseByTid(courseArrange.getTid(), day_id, lesson_id);
		//说明老师在这个时间段已经被安排课程,所以无法在安排
		if (classCourseByTid != null) {
			return false;
		}
		if (classCourse.getSubject() != null) {
			if (Objects.equals(classCourse.getClassid(), courseArrange.getClassid()) && Objects.equals(classCourse.getSubject(), courseArrange.getSubject())) {
				//课程已经被安排了
				return false;
			}
			//如果该课程已经被安排了课程,找到这门课程 减去sign
			CourseArrange temp_ca = adminService.getCourseArrangeByClassSubject(classCourse.getClassid(), classCourse.getSubject(), day_id);
			if (temp_ca.getSign() > 0) {
				bBoolean = adminService.updateCourseArrange(temp_ca.getSign() - 1, temp_ca.getId());
			}

		}
		classCourse.setTid(courseArrange.getTid());
		classCourse.setTname(courseArrange.getTname());
		classCourse.setSubject(courseArrange.getSubject());
		classCourse.setCid(courseArrange.getCid());
		classCourse.setClasses(courseArrange.getClasses());
		classCourse.setCourse_type(courseArrange.getCourse_type());
		Boolean aBoolean = adminService.updateCourseArrange(courseArrange.getSign() + 1, courseArrange.getId());
		Boolean aBoolean1 = adminService.updateClassCourse(classCourse, classId, day_id, lesson_id);
		if ((aBoolean && aBoolean1) && bBoolean) {
			return true;
		} else {
			return false;
		}

	}

	@RequestMapping("/ajax/arrangeClassGuardian")
	@ResponseBody
	public Boolean arrangeClassGuardian(String class_name, Integer tid) {
		ClassInfo classInfo = adminService.getClassByName(class_name);
		Teacher teacher = adminService.getTeacherById(tid);
		classInfo.setGuardian(teacher.getTname());
		Boolean aBoolean = adminService.updateClassGuardian(classInfo.getClassid(), teacher.getTname());
		return aBoolean;
	}


}